package com.edu.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.dao.DispatchDao;
import com.edu.entity.Dispatch;
import com.edu.service.DispatchService;
import com.edu.service.Impl.DispatchServiceImpl;


/**
 * Servlet implementation class DriverServlet
 */
@WebServlet("/DispatchServlet")
public class DispatchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String menu = request.getParameter("menu");
		System.out.println(menu);
		//查询
		if ("1".equals(menu)) {
			queryByDispatch(request,response);
		//修改
		}else if("2".equals(menu)) {
			DispatchUpdate(request, response);
		//添加
		}else if("3".equals(menu)) {
			DispatchAdd(request, response);
		//删除
		}else if("4".equals(menu)) {
			DispatchDelete(request, response);
		}else {
			System.out.println("sorry!");
		}
	}

	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void queryByDispatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int dispatchId = -1;
		if(!("".equals(request.getParameter("dispatchId")))){
			dispatchId = Integer.parseInt(request.getParameter("dispatchId"));
		}
		String tNum = request.getParameter("tNum");
		String dTrigger = request.getParameter("dTrigger");
		String dReach = request.getParameter("dReach");
		String dDepart = request.getParameter("dDepart");
		String dPurpose = request.getParameter("dPurpose");
		String dReturn = request.getParameter("dReturn");
		String dJob = request.getParameter("dJob");
		String dName = request.getParameter("dName");
		String dPhone = request.getParameter("dPhone");
		
		Dispatch dispatch = new Dispatch(dispatchId,tNum,dTrigger,dReach,dDepart,dPurpose,dReturn,dJob,dName,dPhone);
		DispatchService dispatchService = new DispatchServiceImpl();
		List<Dispatch> list = dispatchService.selectByDispatch(dispatch);
		request.setAttribute("list",list);
		request.getRequestDispatcher("Dispatch.jsp").forward(request, response);
	}
	
	protected void DispatchUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		int dispatchId = Integer.parseInt(request.getParameter("dispatchId"));
		String tNum = request.getParameter("tNum");
		String dTrigger = request.getParameter("dTrigger");
		String dReach = request.getParameter("dReach");
		String dDepart = request.getParameter("dDepart");
        String dPurpose = request.getParameter("dPurpose");
		String dReturn = request.getParameter("dReturn");
		String dJob = request.getParameter("dJob");
        String dName = request.getParameter("dName");
		String dPhone = request.getParameter("dPhone");
		
		Dispatch dispatch = new Dispatch(dispatchId,tNum,dTrigger,dReach,dDepart,dPurpose,dReturn,dJob,dName,dPhone);
		DispatchService dispatchService = new DispatchServiceImpl();
		
		dispatchService.updateDispatch(dispatch);
		response.sendRedirect("Dispatch.jsp");
	}
	protected void DispatchAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tNum = request.getParameter("tNum");
		String dTrigger = request.getParameter("dTrigger");
		String dReach = request.getParameter("dReach");
		String dDepart = request.getParameter("dDepart");
        String dPurpose = request.getParameter("dPurpose");
		String dReturn = request.getParameter("dReturn");
		String dJob = request.getParameter("dJob");
        String dName = request.getParameter("dName");
		String dPhone = request.getParameter("dPhone");
		Dispatch dispatch = new Dispatch(tNum,dTrigger,dReach,dDepart,dPurpose,dReturn,dJob,dName,dPhone);
		DispatchService tcheckService = new DispatchServiceImpl();
		
		tcheckService.addDispatch(dispatch);
		response.sendRedirect("Dispatch.jsp");
	}
	protected void DispatchDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String[] ids = request.getParameterValues("selectdispatchId");
		DispatchService dispatchService = new DispatchServiceImpl();
		
		dispatchService.deleteDispatchAll(ids);
		response.sendRedirect("Dispatch.jsp");	
	}
}
